package com.bh.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 拼音
 *
 * @author shuangluxing
 * @date 2023/03/09
 */
public class PinyinUtils {

    HanyuPinyinOutputFormat format = null;

    public enum Type {
        UPPERCASE,              //全部大写
        LOWERCASE,              //全部小写
        FIRSTUPPER,              //首字母大写
        ONLYFIRSTCHAR    //只有首字母--大写
    }

    static PinyinUtils record = new PinyinUtils();

    public PinyinUtils() {
        format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    /**
     * 首字母大写    明天--Ming tian
     *
     * @param str
     * @param spera
     * @return
     */
    public static String toPinYinFirstUpper(String str, String spera) {
        String pinYin;
        try {
            pinYin = record.toPinYin(str, spera, Type.FIRSTUPPER);
            if ("0123456789".contains(pinYin)) {
                pinYin = numBerToPinyin(pinYin);
            }
            return pinYin;
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return "";
        }
    }

    /**
     * 全部大写        明天--MINGTIAN
     *
     * @param str
     * @param spera
     * @return
     */
    public static String toPinYin(String str, String spera) {
        String pinYin;
        try {
            pinYin = record.toPinYin(str, spera, Type.UPPERCASE);
            return pinYin;
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return "";
        }
    }

    /**
     * 只包含首字母，大写    明天 --MT
     *
     * @param str
     * @return
     */
    public static String onlyFirstCharUpper(String str) {
        String pinYin;
        try {
            pinYin = record.toPinYin(str, " ", Type.FIRSTUPPER);
            if (pinYin.length() == 0) {
                return "";
            }
            String[] array = pinYin.split(" ");
            StringBuilder firstchar = new StringBuilder();
            for (String ste : array) {
                if (ste.length() > 0) {
                    firstchar.append(ste.substring(0, 1).toUpperCase());
                }
            }
            return firstchar.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return "";
        }
    }

    /**
     * 如： 明天 转换成 MINGTIAN
     *
     * @param str
     * @param spera
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
        if (str == null || str.trim().length() == 0) {
            return "";
        }
        if (type == Type.UPPERCASE) {
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        } else {
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        }
        StringBuilder py = new StringBuilder();
        String temp = "";
        String[] t;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if ((int) c <= 128) {
                py.append(c);
            } else {
                t = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (t == null) {
                    py.append(c);
                } else {
                    temp = t[0];
                    if (type == Type.FIRSTUPPER) {
                        temp = t[0].toUpperCase().charAt(0) + temp.substring(1);
                    }
                    py.append(temp).append(i == str.length() - 1 ? "" : spera);
                }
            }
        }

        return py.toString().trim();
    }

    public static String getFirstSpell(String chinese) {
        StringBuilder pybf = new StringBuilder();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : arr) {
            if (c > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
                    if (temp != null) {
                        pybf.append(temp[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(c);
            }
        }
        return pybf.toString().replaceAll("\\W", "").trim();
    }

    /**
     * 数字转换成对应拼音
     * @param str
     * @return
     */
    public static String numBerToPinyin(String str) {
        String pinyin = null;
        switch (str) {
            case "0":
                pinyin = "Z";
                break;
            case "1":
                pinyin = "Y";
                break;
            case "2":
                pinyin = "E";
                break;
            case "3":
                pinyin = "S";
                break;
            case "4":
                pinyin = "S";
                break;
            case "5":
                pinyin = "W";
                break;
            case "6":
                pinyin = "L";
                break;
            case "7":
                pinyin = "Q";
                break;
            case "8":
                pinyin = "B";
                break;
            case "9":
                pinyin = "J";
                break;
        }
        return pinyin;

    }


    /**
     * 得到 全拼
     *
     * @param src
     * @return
     */
    public static String getPingYin(String src) {
        char[] t1;
        t1 = src.toCharArray();
        String[] t2;
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuilder t4 = new StringBuilder();
        try {
            for (char c : t1) {
                // 判断是否为汉字字符
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(c, t3);
                    t4.append(t2[0]);
                } else {
                    t4.append(Character.toString(c));
                }
            }
            return t4.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4.toString();
    }


    /**
     * 得到中文首字母
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString();
    }


    /**
     * 将字符串转移为ASCII码
     *
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {
        StringBuilder strBuf = new StringBuilder();
        byte[] bytes = cnStr.getBytes();
        for (byte b : bytes) {
            strBuf.append(Integer.toHexString(b & 0xff));
        }
        return strBuf.toString();
    }


    public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
        System.out.println(onlyFirstCharUpper("赵波蓝"));
    }
}

